// pages/shop/zfb.js
var app = getApp();
Page({
  data: {
    withDrawtypes: [
      {
        types: 1,
        names: '微信提现'
      }
    ]
  },
  onLoad: function (options) {
    var that = this;
    app.func.req('shops/show', {
      id: wx.getStorageSync('shop').id
    }, 'GET', function (res) {
      var item = res.result;
      if (item.amount == null) {
        item.amount = 0;
      }
      that.setData({
        shop: item
      });
    }, {
        'token': wx.getStorageSync('token')
      });

    app.util.appInfo(app, function(res){
      var withDrawtypes = that.data.withDrawtypes;
      if (res.result.alipay_switch == 1) {
        withDrawtypes = [
          {
            types: 1,
            names: '微信提现'
          },
          {
            types: 2,
            names: '支付宝提现'
          }
        ]
      }
      that.setData({
        app_kfphone: res.result.kfphone,
        app_kfwechat: res.result.kfwechat,
        app_withdraw_min_amount: res.result.withdraw_min_amount,
        app_withdraw_daily_times: res.result.withdraw_daily_times,
        withDrawtypes: withDrawtypes,
        is_alipay: res.result.alipay_switch
      })
    });
  },
  bindAccount: function (e) {
    var account = e.detail.value.account;
    var wxaccount = e.detail.value.wxaccount;
    var name = e.detail.value.name;
    var shop_id = this.data.shop.id;
    var that = this;
    var shop = this.data.shop;
    app.func.req('shops/update', {
      id: shop_id,
      alipay_account: account,
      alipay_name: name,
      wxpay_account: wxaccount
    }, 'POST', function (res) {
      if (res.code == 'success') {
        wx.showToast({
          title: '保存成功',
          duration: 3000
        });
        that.onLoad();
      }
    }, {
        'token': wx.getStorageSync('token')
      });
  },
  withDraw: function (e) {
    var that = this
    var shop = this.data.shop;
    var formId = this.data.formId;
    var types = e.target.dataset.types
    var pay_types = this.data.withDrawtypes[e.detail.value].types;
    if (pay_types == 2 && (!shop.alipay_account || !shop.alipay_name)){
      wx.showToast({
        title: '请绑定支付宝',
        image: '/images/fail.png',
        duration: 2000
      })
      return
    } else if (pay_types == 1 && !shop.wxpay_account){
      wx.showToast({
        title: '请绑定微信号',
        image: '/images/fail.png',
        duration: 2000
      })
      return
    }
    app.func.req('shops/withdraw', {
      shop_id: shop.id,
      types: types,
      pay_types: pay_types,
      form_id: formId
    }, 'POST', function (res) {
      if (res.code == "success") {
        wx.showToast({
          title: '已提交，等待处理',
          icon: 'success',
          duration: 2000
        })
        setTimeout(function () {
          wx.redirectTo({
            url: './stat'
          })
        }, 2000)
      } else {
        var error_title;
        if (res.msg == "withdraw.shop.invalid") {
          error_title = "最低提现金额" + that.data.app_withdraw_min_amount + "元"
        } else if (res.msg == "shop.withdraw.too_many_times_today") {
          error_title = "每日最多提现"+ that.data.app_withdraw_daily_times + "次"
        } else {
          error_title = "提交申请失败"
        }
        wx.showModal({
          title: '提示',
          content: error_title,
          showCancel: false,
          confirmText: '朕知道了',
        })
      }
    }, {
        token: wx.getStorageSync('token')
      })
  },
  setformId: function (e) {
    this.setData({
      formId: e.detail.formId
    })
  },
  telPhone: function () {
    var that = this;
    wx.makePhoneCall({
      phoneNumber:  that.data.app_kfphone
    })
  }
})
